﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace MediaWiki.Office.Model
{
  public class Run : InlineContent
  {
    public const string LocalName = "r";
    public static readonly XName Name = Document.WordprocessingML + LocalName;

    private RunProperties _properties;
    private List<RunContent> _content = new List<RunContent>();

    private string _openDocumentStyleId;

    public string OpenDocumentStyleId
    {
      get { return _openDocumentStyleId; }
      set { _openDocumentStyleId = value; }
    }

    public OpenDocumentStyle OpenDocumentStyle
    {
      get { return MainDocument.OpenDocumentStyles[_openDocumentStyleId]; }
    }

    public List<RunContent> Content
    {
      get { return _content; }
      set { _content = value; }
    }

    public RunProperties Properties
    {
      get { return _properties; }
      set { _properties = value; }
    }

    public Run(Paragraph parent, XElement element)
      : base(parent, element)
    {
      XNamespace ns = element.Name.Namespace;

      _openDocumentStyleId = (string)element.Attribute(OpenDocumentStyle.Text + "style-name");

      _properties = new RunProperties(this, element.Element(ns + "rPr"));

      _content.AddRange(RunContent.Create(this, element.Elements()));
    }

    public override InlineContentType ContentType
    {
      get { return InlineContentType.Run; }
    }

    public override string ToString()
    {
      StringBuilder sb = new StringBuilder();

      foreach (RunContent content in _content)
      {
        sb.Append(content.ToString());
      }

      return sb.ToString();
    }
  }
}
